﻿using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace QQSDK
{
    /// <summary>
    /// 有起屏广告测试场景
    /// </summary>
    public class SplashADMainSample : MonoBehaviour
    {

        public GameObject PrivacyDialog;
        private AsyncOperation async;
        public GameObject splashAD;
        public Button skipBtn;
        //需要跳转的场景名称
        public string nextSceneName;
        /// <summary>
        /// Awake is called when the script instance is being loaded.
        /// </summary>
        void Start()
        {
            if (string.IsNullOrEmpty(nextSceneName))
            {
                SDKFunc.LogError("请设置下一个要显示的场景名称");
                return;
            }
            SDKManager.Init();
            //异步加载下一个场景
            async = SceneManager.LoadSceneAsync(nextSceneName);
            async.allowSceneActivation = false;

#if UNITY_EDITOR || DEVELOP

#elif UNITY_IOS && !DELSDK
        Log("用户的IDFA" + ToolsManager.GetIOSIDFA());
#elif UNITY_ANDROID
        AdsManager.instance.onGetAdvertismentID += (advertismentID) =>
        {
            Log("用户的广告ID" + advertismentID);
        };
#endif
            AdsManager.instance.onImpressionDataCallback += (data) =>
            {
                Log("广告展示数据：" + data);
            };
            //监听广告初始化成功的回调事件
            AdsManager.instance.onInitAdsSucceed += () =>
            {
                ToolsManager.ShowGDPR("http://file.cdn.tapque.com/Innovate/Makeup/Makeup_Privacy_policy.html");

                AdsManager.instance.LoadSplash();
            };
            AdsManager.instance.onInitAdsFailed += () =>
            {
                Log("广告初始化失败，加载下一个场景");
                async.allowSceneActivation = true;
            };
            //开屏广告正在请求中
            AdsManager.instance.onSplashRequest += () => { };
            //开屏广告请求失败
            AdsManager.instance.onSplashLoaded += () =>
            {
                //直接加载下一个场景
                SDKFunc.Log("开屏加载成功");
                AdsManager.instance.ShowSplash();
                async.allowSceneActivation = true;
            };
            AdsManager.instance.onSplashLoadedFailed += () =>
            {
                //开屏广告加载失败 直接加载下一个场景
                SDKFunc.Log("开屏加载失败");
                async.allowSceneActivation = true;
            };
            AdsManager.instance.onSplashImpression += () =>
            {
                //开屏广告展示
                SDKFunc.Log("开屏展示成功");
            };
        }
        /// <summary>
        /// Ons the click accept button. 用户同意gdpr
        /// </summary>
        public void OnClickPrivacyBtn(bool status)
        {
            PrivacyDialog.SetActive(false); //隐藏弹窗
            async.allowSceneActivation = true; //直接进行场景跳转
                                               //测试代码，只在编辑器中运行
#if UNITY_EDITOR || DEVELOP
            //TimerSchedule.Schedule(this, 6, () =>
            //{
            //    async.allowSceneActivation = true;
            //    TimerSchedule.Cancel();
            //});

            Invoke("allowSceneActivation", 6);
#endif
        }

        public void allowSceneActivation()
        {
            async.allowSceneActivation = true;
            //TimerSchedule.Cancel();
        }


        public void OnClickSkipBtn()
        {
            async.allowSceneActivation = true;
        }
        private void Log(object obj)
        {
            SDKFunc.Log("Unity OM:" + obj.ToString());
        }
        public void ClickOnUseOfTerms()
        {
            Application.OpenURL("https://x-flow.app/terms-of-use.html");
        }
        public void ClickOnPrivacyPolicy()
        {
            Application.OpenURL("https://x-flow.app/privacy-policy.html");
        }
    }
}